#include "Mesh.h"
#include "Texture.h"
#include "neiderra/core/Defs.h"
#include "neiderra/math/Misc.h"

using namespace neiderra::core;
using namespace neiderra::rendering;

Mesh::Mesh(): RenderingObject(0) 
{
	_maxSize = 0;
	_texture = 0;
	_normals = 0;
	_maplist = 0;
	_scaleFactor = 1;
	faces_ = 0;
	vertices_= 0;
	facenum_ = 0;
	vertnum_ = 0;
	_mapnum = 0;
}

Mesh::Mesh(Core* core): RenderingObject(core) 
{
	_maxSize = 0;
	_scaleFactor = 1;
	_texture = 0;
	_normals = 0;
	_maplist = 0;
	faces_ = 0;
	vertices_= 0;
	_mapnum = 0;
	facenum_ = 0;
	vertnum_ = 0;
}

void Mesh::setFaces(face* fp, unsigned int n)
{
	faces_ = fp;
	facenum_ = n;
}

void Mesh::setVertices(vertex* vp, unsigned int n)
{
	vertices_ = vp;
	vertnum_ = n;
}

void Mesh::setTexture(Texture *texture)
{
	_texture = texture;
}

unsigned Mesh::getMaxSize() { return _maxSize; }

void Mesh::setMaxSize(unsigned x) { 
	_maxSize = x;
}

void Mesh::getMaxDims(vec3& v)
{
	v = _maxDims;
}

void Mesh::scaleToMaxSize()
{
	//if(!_maxSize)
	//	return;

	//if(!vertnum_)
	//	return;

	//if(!vertices_)
	//	return;

	//// determine scale factor
	//float maxSize=0, finalMax=0;
	//for(unsigned i=0;i<vertnum_;i++)
	//{
		//maxSize = math::max(vertices_[i].x, vertices_[i].y);
		//maxSize = math::max(maxSize, vertices_[i].z);

	//	if(finalMax < maxSize)
	//		finalMax = maxSize;
	//}

	//float Cm = _maxSize/finalMax;

	//// scaling
	//for(unsigned i=0;i<vertnum_;i++){
	//	vertices_[i].x *= Cm;
	//	vertices_[i].y *= Cm;
	//	vertices_[i].z *= Cm;
	//}
}
	
Mesh::~Mesh()
{
	delete[] _localTransform;
	delete[] vertices_;
	delete[] _maplist;
	delete[] _normals;
	delete[] faces_;
}
